package com.company.app.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.company.app.model.ProductTypeModel;
import com.company.app.service.ProductTypeService;

/**
 * The Class ProductTypeAction.
 */
public class ProductTypeAction extends AbstractBaseAction<ProductTypeModel> {
	
	/** The Constant LOG. */
	private static final Logger LOG = LoggerFactory.getLogger(ProductTypeAction.class);

	/** The product type service. */
	@SpringBean("productTypeServiceClient")
	private ProductTypeService productTypeService;

	/** The Constant INIT_VIEW. */
	private static final String INIT_VIEW = "/jsp/productType.jsp";

	/**
	 * Default method.
	 *
	 * @return the resolution
	 */
	@DefaultHandler
	public Resolution defaultMethod() {
		return new ForwardResolution(INIT_VIEW);
	}

	/**
	 * Save product type.
	 *
	 * @return the resolution
	 */
	public Resolution saveProductType() {
		/*
		 * ** TODO IMPLEMENT STRIPES VALIDATIONS
		 * 
		 * if (this.getModel().getProductView() == null) { ValidationError error
		 * = new
		 * LocalizableError("com.company.app.action.ProductAction.model.productView"
		 * ); this.getContext().getValidationErrors().add("user", error); return
		 * this.getContext().getSourcePageResolution(); } if
		 * (this.getModel().getProductView().getCode()==null){ ValidationError
		 * error = new LocalizableError(
		 * "com.company.app.action.ProductAction.model.productView.code");
		 * this.getContext().getValidationErrors().add("code", error); return
		 * this.getContext().getSourcePageResolution(); }
		 */
		if (!this.isProductTypeValid()) {
			this.getContext().getResponse().setStatus(501);
			LOG.error("Product invalid");
		} else {
			if (this.getModel().getProductType().getId() != null) {
				this.productTypeService.updateProductType(this.getModel()
						.getProductType());
			} else {
				this.productTypeService.createProductType(this.getModel()
						.getProductType());
			}
		}
		//
		return new ForwardResolution(INIT_VIEW);
	}

	/**
	 * Delete product type.
	 *
	 * @return the resolution
	 */
	public Resolution deleteProductType() {
		/*
		 * ** TODO IMPLEMENT STRIPES VALIDATIONS
		 * 
		 * if (this.getModel().getProductView().getId() == null) {
		 * ValidationError error = new
		 * LocalizableError("com.company.app.action.ProductAction.model.productView"
		 * ); this.getContext().getValidationErrors().add("user", error); return
		 * this.getContext().getSourcePageResolution(); }
		 */
		int responseStatus = 0;
		if (this.getModel().getProductType().getId() != null) {
			if (!this.productTypeService.deleteProduct(this.getModel()
					.getProductType())) {
				responseStatus = 500;
			}
		}
		if (responseStatus > 0) {
			this.getContext().getResponse().setStatus(responseStatus);
		}
		return new ForwardResolution(INIT_VIEW);
	}

	/**
	 * Checks if is product type valid.
	 *
	 * @return true, if is product type valid
	 */
	public boolean isProductTypeValid() {
		boolean isValid = true;
		if (this.getModel().getProductType() == null) {
			isValid = false;
		}
		return isValid;

	}

	/**
	 * Sets the product type service.
	 *
	 * @param productTypeService the new product type service
	 */
	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
	
	

}
